// Mini.cpp : implementation file
//

#include "stdafx.h"
#include "moretunes.h"
#include "Mini.h"
#include ".\mini.h"

// CMini dialog

IMPLEMENT_DYNAMIC(CMini, CDialog)

CMini::CMini(CWnd* pParent, CAllTabs *allTabs, CPrefTab *prefs)
	: CDialog(CMini::IDD, pParent),
	_currentAlbum(NULL)
	, _allTabs(allTabs)
	, _prefs(prefs)
//	, _drag(0)
{
}

CMini::~CMini()
{
}

void CMini::DoDataExchange(CDataExchange* pDX) {
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_MINI, _pic);
	DDX_Control(pDX, IDC_EDIT1, _status);
	DDX_Control(pDX, IDC_MINI_REGISTER, _register);
}


BEGIN_MESSAGE_MAP(CMini, CDialog)
	ON_WM_RBUTTONUP()
	ON_WM_WINDOWPOSCHANGED()
	ON_WM_PAINT()
	ON_WM_CTLCOLOR()
	ON_WM_NCHITTEST()
	ON_WM_HELPINFO()
//	ON_WM_RBUTTONDBLCLK()
//	ON_WM_LBUTTONDBLCLK()
//	ON_WM_LBUTTONDOWN()
//	ON_WM_LBUTTONUP()
END_MESSAGE_MAP()


// CMini message handlers
#define COVER_NOT_AVAIL "Cover not available"
#define ARTIST_NOT_FOUND "Artist not found"
int CMini::setResult(Album* album) {
	_currentAlbum = album;


	Invalidate();
	UpdateWindow();
	return TRUE;
}

BOOL CMini::OnInitDialog()
{
	CDialog::OnInitDialog();

	HICON icon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	SetIcon(icon, TRUE); // Set big icon
	if (_prefs->_registration == REGISTRATION_OK)
		_register.ShowWindow(SW_HIDE);
	return TRUE;  // return TRUE unless you set the focus to a control
}
#define KEY_STATE_UP 257
BOOL CMini::PreTranslateMessage(MSG* pMsg) {
	if(pMsg->wParam == VK_F2 && pMsg->message == KEY_STATE_UP) {
		_allTabs->OnShowMainWindow();
	}
	else if(pMsg->wParam == VK_F6 && pMsg->message == KEY_STATE_UP) {
		_allTabs->_albumsTab->OnLyrics();
	}
	else if(pMsg->wParam == VK_F5 && pMsg->message == KEY_STATE_UP) {
		_allTabs->_albumsTab->OnReload();
	}
	return CDialog::PreTranslateMessage(pMsg);
}
/* TEST
FAILED
mini window : move the around, dual screen environment
OK
main window : move the around
mini window : move the around
main window : check if the position is saved
mini window : check if the position is saved
*/
void CMini::OnWindowPosChanged(WINDOWPOS* lpwndpos) {
	CDialog::OnWindowPosChanged(lpwndpos);
	if (_prefs == NULL)
		return;
	// Have to get window size here!
	CRect fullRect;
	GetWindowRect(fullRect); // absolute position
	int maxX = GetSystemMetrics(SM_CXVIRTUALSCREEN) - (fullRect.bottom - fullRect.top) - 4;
	int maxY = GetSystemMetrics(SM_CYVIRTUALSCREEN) - (fullRect.right - fullRect.left);
	if (lpwndpos->y < 0)
		lpwndpos->y = 0;
	if (lpwndpos->x < -3000 || lpwndpos->x > 3000)
		lpwndpos->x = 0;
	if (lpwndpos->y > maxY)
		lpwndpos->y = maxY;
	//if (lpwndpos->x > maxX) // don't need that, cause i lose dual screen support!
	//	lpwndpos->x = maxX;
	SetWindowPos(this, lpwndpos->x, lpwndpos->y, 0, 0, SWP_NOZORDER|SWP_NOSIZE);
	_prefs->_miniTop = lpwndpos->y;
	_prefs->_miniLeft = lpwndpos->x;
	_prefs->saveData();
}

void CMini::OnPaint() {
	CPaintDC dc(this); // device context for painting
	_status.SetWindowText("moreTunes");
	if (_currentAlbum == NULL) {
		_pic.loadPic("");
		_status.ShowWindow(SW_SHOW);
		_status.SetWindowText(ARTIST_NOT_FOUND);
	}
	else {
		CFile fp;
		if (fp.Open(_currentAlbum->getImageFile(), CFile::modeRead)) {
			if (fp.GetLength() > 1000) {
				_status.ShowWindow(SW_HIDE);
				fp.Close();
				_pic.loadPic(_currentAlbum->getImageFile());
			}
			else {
				_pic.loadPic("");
				fp.Close();
			}
		}
		else
			_pic.loadPic("");
	}
	if (_prefs->_registration == REGISTRATION_OK)
		_register.ShowWindow(SW_HIDE);
}

HBRUSH CMini::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
	HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
	if (nCtlColor==CTLCOLOR_DLG) {
		pDC->SetBkMode(TRANSPARENT);
		hbr = CreateSolidBrush(RGB(0,0,0));
	}
	return hbr;
}
void CMini::OnRButtonUp(UINT nFlags, CPoint point) {
	CMenu menu ;
	VERIFY(menu.LoadMenu(IDR_MENU_MINI));
	CMenu* pPopup = menu.GetSubMenu (0) ;
	ASSERT(pPopup != NULL);
	// Get the cursor position
	POINT pt ;
	GetCursorPos (&pt) ;
	pPopup->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON,pt.x, pt.y, AfxGetMainWnd());
	CDialog::OnLButtonUp(nFlags, point);
}
UINT CMini::OnNcHitTest(CPoint point) {
	if (GetKeyState(VK_RBUTTON) < 0)
		return CDialog::OnNcHitTest(point);	
	if (GetKeyState(VK_LBUTTON) < 0) {
		_allTabs->OnShowMainWindow();
		return CDialog::OnNcHitTest(point);	
	}
	return HTCAPTION;
}

BOOL CMini::OnHelpInfo(HELPINFO* pHelpInfo) {
	HtmlHelp(NULL, NULL);
	return NULL;
}

